package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.exception.CustomerException;
import com.gxa.exception.MessageCode;
import com.gxa.jd.interceptor.UserThreadLocal;
import com.gxa.jd.mapper.OrderMapper;
import com.gxa.jd.pojo.Hotel;
import com.gxa.jd.pojo.Order;
import com.gxa.jd.pojo.Room;
import com.gxa.jd.pojo.User;
import com.gxa.jd.service.HotelService;
import com.gxa.jd.service.OrderService;
import com.gxa.entity.PageResult;
import com.gxa.jd.service.RoomService;
import com.gxa.jd.vo.OrderVo;
import com.gxa.jd.vo.OrderVoo;
import com.gxa.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RoomService roomService;

    @Autowired
    private HotelService hotelService;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<OrderVoo> findAll(Long id) {

        return orderMapper.findByUserId(id);
    }

    @Override
    public List<OrderVoo> findAllByState(Long id, Integer state) {
        return orderMapper.findByS(id,state);
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Order> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Order> orders = (Page<Order>) orderMapper.selectAll();
        return new PageResult<Order>(orders.getTotal(), orders.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Order> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Order> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Order> orders = (Page<Order>) orderMapper.selectByExample(example);
        return new PageResult<Order>(orders.getTotal(), orders.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param
     * @return
     */
    @Override
    public OrderVo findByOrderNum(String orderNum) {

        return orderMapper.findOrderVoById(orderNum);
    }

    /**
     * 新增
     *
     * @param order
     */
    @Override
    public void add(Order order) {
        //自动生成订单号
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            sb.append(new Random().nextInt(10));
        }
        String num = sb.toString();
        Room room = roomService.findById(order.getRoomId());
        if(room==null){
            throw new CustomerException(MessageCode.ROOM);
        }
        Hotel hotel = hotelService.findById(order.getHotelId().intValue());
        if (hotel==null){
            throw new CustomerException(MessageCode.HOTEL);
        }
        order.setMoney(room.getPrice()*order.getCheckInNum()*order.getReserveNum());
        //TODO 减库存
        User user = UserThreadLocal.get();
        order.setUserId(user.getId());
        order.setOrderNum(num);
        order.setSubmissionTime(new Date());
        //设置订单默认状态为未支付状态
        order.setState(0);
        //设置订单超时时间
        order.setLastPayTime(new Date(new Date().getTime()+3600000));
        orderMapper.insert(order);
    }



    /**
     * 修改
     *
     * @param order
     */
    @Override
    public void update(Order order) {

        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 删除
     *
     * @param
     */
    @Override
    public void delete(String orderNum) {
        orderMapper.deleteByPrimaryKey(orderNum);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */

    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 订单编号
            if (searchMap.get("orderNum") != null && !"".equals(searchMap.get("orderNum"))) {
                criteria.andLike("orderNum", "%" + searchMap.get("orderNum") + "%");
            }
            // 入住人姓名
            if (searchMap.get("checkInPerson") != null && !"".equals(searchMap.get("checkInPerson"))) {
                criteria.andLike("checkInPerson", "%" + searchMap.get("checkInPerson") + "%");
            }
            // 入住人手机
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                criteria.andLike("phone", "%" + searchMap.get("phone") + "%");
            }
            // 留言
            if (searchMap.get("message") != null && !"".equals(searchMap.get("message"))) {
                criteria.andLike("message", "%" + searchMap.get("message") + "%");
            }

            // 入住天数
            if (searchMap.get("checkInNum") != null) {
                criteria.andEqualTo("checkInNum", searchMap.get("checkInNum"));
            }
            // 预定间数
            if (searchMap.get("reserveNum") != null) {
                criteria.andEqualTo("reserveNum", searchMap.get("reserveNum"));
            }

        }
        return example;
    }

}
